//
//  ViewController.m
//  UIKit_Photos_Sketch
//
//  Created by 李竞 on 16/1/24.
//  Copyright © 2016年 lionjing. All rights reserved.
//

#import "ViewController.h"
#import "LJ_photo.h"

@interface ViewController ()

/** 内存管理的技术点 之 strong , weak 的选用: 
 在OC中,如果对象没有强引用,会被立即释放 
 默认的对象,都是强引用的 .
 拖到控制器中的控件,可以直接用weak ,避免重复引用
 但是如果没有被多个视图强引用,那么就只能用strong
 注 :对于NSString,block我们建议使用copy

 */
@property (weak, nonatomic) IBOutlet UIImageView *imgView;

@property (weak, nonatomic) IBOutlet UIButton *btnLeft;

@property (weak, nonatomic) IBOutlet UIButton *btnRight;

@property (weak, nonatomic) IBOutlet UILabel *disLabel; //文字描述 ;

@property (weak, nonatomic) IBOutlet UILabel *discripLabel; //数字描述 ;


@property (nonatomic , strong) NSMutableArray *arrayIMG ;

@property (nonatomic , assign) NSInteger index ;

@end

@implementation ViewController

-(NSMutableArray *)arrayIMG{
    if (!_arrayIMG) {

//        [NSBundle mainBundle]对应的是应用程序包的路径.
        NSString *path = [[NSBundle mainBundle] pathForResource:@"LJ_IMG.plist" ofType:nil];

        NSArray *array_plist = [NSArray arrayWithContentsOfFile:path];
        NSMutableArray *array_M = [NSMutableArray arrayWithCapacity:array_plist.count];
        for (NSDictionary *dict in array_plist) {
            LJ_photo *photo = [LJ_photo initWith_Dict:dict];

            [array_M  addObject:photo];
        }
        _arrayIMG = array_M ;
    }
    return _arrayIMG ;
}

//左按钮
-(void)btn_Left_Click{
    self.index -= 1;
    NSLog(@"Left");
    [self btn_enabled_Bool];

}


//右按钮
-(void)btn_Right_Click{

    self.index += 1 ;
    NSLog(@"Right");
    [self btn_enabled_Bool];

}

//控制按钮enabled 属性 :
-(void)btn_enabled_Bool{

    LJ_photo *photo = self.arrayIMG[_index] ;
    //设置图片 :
    self.imgView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@", photo.name]];
    //设置文字 :
    self.disLabel.text = photo.desc ;
    //设置数字标号 :
    self.discripLabel.text = [NSString stringWithFormat:@"%ld / %ld",_index + 1 , (unsigned long)self.arrayIMG.count ] ;
    //控制按钮是否可点击 :
    self.btnLeft.enabled = self.index != 0 ;
    self.btnRight.enabled = self.index != self.arrayIMG.count -1 ;

    NSLog(@"当前是第: %ld 张图片", (long)_index + 1);
}




- (void)viewDidLoad {
    [super viewDidLoad];
    //添加点击事件 :
    [self.btnLeft addTarget:self action:@selector(btn_Left_Click) forControlEvents:UIControlEventTouchUpInside];
    [self.btnRight addTarget:self action:@selector(btn_Right_Click) forControlEvents:UIControlEventTouchUpInside];

    [self btn_enabled_Bool];


    }



- (void)didReceiveMemoryWarning {[super didReceiveMemoryWarning];}
@end
